﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Windows;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using FLTVAGSAB;
using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    public partial class FLTVAGSABData : BaseFunction
    {
        public string ITXMANDANT = "00", ITXDBVERSION = "50.24", ITXUSER = "INTEXMAP", ITXDATE = "20120723";
        public string ITXLOOMTYPE = "T610  ";
        public string ITXFART = "                    ", ITXGART = "                    ", ITXBART = "                    ";
        public string ITXFVAR = "  ", ITXGVAR = "  ", ITXBVAR = "1 ";
        public string ITXFCOLOR = "          ", ITXGCOLOR = "          ", ITXBCOLOR = "          ";
        public string ITXFFINISH = "          ", ITXCADID = "1";
        public string ITXWBLO = "TX", ITXWBLB = "01";

        public string CADCHAR = " ", ITXCHAR = " ";
        DataRow dwIntexStd = null;
        public Dictionary<string, object> CADTable = new Dictionary<string, object>();
        private Convert2Intex Convert2Intex;
        public DataSet SetExt;
        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public FLTVAGSABData()
            : base()
        {
        }
        public FLTVAGSABData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            SetExt = new DataSet();
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public FLTVAGSABData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        #region=============INTEX WS/BS=============================
        public DataSet ResetSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.xls");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                }
                setResult = exAgent.LoadDataTableDef(filePath, "");
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                setResult.WriteXml(filePath);
                setResult = new DataSet();
                setResult = LoadSetDispo(setID);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                setResult.ReadXml(filePath);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveSetDispo(DataTable tbDispo)
        {
            DataSet setDispo = new DataSet();

            try
            {
                setDispo.Merge(tbDispo);
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\DATADISPO.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                    setDispo.WriteXml(filePath);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        public DataSet ConvertToIntex(ref bool success, ref string failreason)
        {
            DataSet setResult = new DataSet();
            DataSet Return = new DataSet();//is used to magage the data to upload to intex 
            DataTable tbGSCOLOR = new DataTable();

            try
            {
                Convert2Intex = new Convert2Intex(SetExt, dwIntexStd, OracleDB, proxy);
                dwIntexStd = Convert2Intex.dwIntexStd;

                #region=======VK==========
                #endregion

                #region=====VAART_ITX==========
                if (SetExt.Tables["VAFARBEN_EXT"].Rows.Count > 0)
                {
                    setResult = Convert2GSCOLOR();
                    SetExt.Merge(setResult);
                    setResult = Convert2GASTGA();
                    SetExt.Merge(setResult);
                    setResult = Convert2GZKON();
                    SetExt.Merge(setResult);    
                    setResult = Convert2VAFARBEN();
                    SetExt.Merge(setResult);
                    Return.Merge(SetExt.Tables["GSCOLOR"]);
                    Return.Merge(SetExt.Tables["GASTGA"]);
                    Return.Merge(SetExt.Tables["GZKON"]); 
                    Return.Merge(SetExt.Tables["VAFARBEN"]);
                }
                #endregion
                if (SetExt.Tables["ABPOS_EXT"].Rows.Count > 0)
                {
                    setResult = Convert2ABPOS();
                    SetExt.Merge(setResult);
                    Return.Merge(SetExt.Tables["ABPOS"]);
                    Return.Merge(SetExt.Tables["ABKOPF"]);
                    Return.Merge(SetExt.Tables["ABPR"]);
                    Return.Merge(SetExt.Tables["ABAUFM"]);
                    Return.Merge(SetExt.Tables["GAAUF"]);
                    Return.Merge(SetExt.Tables["ABFOLGE"]);
                }
        
                /*
                Return.Merge(SetExt.Tables["VKPOS"]);
                Return.Merge(SetExt.Tables["VKKOPF"]);
                if (SetExt.Tables.Contains("WBBST"))
                {
                    Return.Merge(SetExt.Tables["WBBST"]);
                    Return.Merge(SetExt.Tables["WDECK"]);
                    Return.Merge(SetExt.Tables["WBBEW"]);
                    Return.Merge(SetExt.Tables["VVLIEF"]);
                    Return.Merge(SetExt.Tables["VVTLIEF"]);
                    Return.Merge(SetExt.Tables["VVPACK"]);
                    Return.Merge(SetExt.Tables["WGBST"]);
                }*/
                //VKPOS VKKOPF WBBST WBBEW VVLIEF VVTLIEF
            }
            catch (Exception ex)
            {
                success = false;
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
    }
}